﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace SCG.TurboSprite.TileBased
{
    [ToolboxBitmap(typeof(TileBasedNavigator), "TileBasedNavigator")]
    public partial class TileBasedNavigator : SpriteSurfaceNavigator
    {
        //constructors
        public TileBasedNavigator()
        {
            InitializeComponent();
        }
        public TileBasedNavigator(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }

        //Need to change bitmap on resize
        public void RefreshMap()
        {         
            _bmp = null;
            Refresh();
        }

        //Do our custom rendering
        protected override void OnCustomPaint(PaintEventArgs e)
        {
            TileBasedSpriteSurface t = SpriteSurface as TileBasedSpriteSurface;
            if (t.MapSet == null)
                return;

            //create the bitmap
            if (_bmp == null)
            {
                _bmp = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                Graphics gr = Graphics.FromImage(_bmp);
                using (gr)
                {
                    gr.Clear(BackColor);                
                    lock (t.MapSet.Tiles)
                    {
                        foreach (Tile tile in t.MapSet.Tiles)
                        {
                            if (tile.TileType == TileType.Sea)
                                continue;
                            Point[] pts = new Point[tile.Points.Count];
                            for (int i = 0; i < tile.Points.Count; i++)
                                pts[i] = new Point((int)(tile.Points[i].X * XMult), (int)(tile.Points[i].Y * YMult));
                            _brush.Color = tile.Color;
                            gr.FillPolygon(_brush, pts);
                        }
                    }
                }
            }

            //Draw it
            e.Graphics.DrawImage(_bmp, 0, 0);
        }

        //private members
        private Bitmap _bmp = null;
        private SolidBrush _brush = new SolidBrush(Color.Black);
    }
}
